export function formatTime1(timestap: number | undefined): string {
  if (typeof timestap !== "number") {
    return "";
  }
  let result = "";
  const date = new Date(timestap);
  const YY = date.getFullYear();
  const MM = date.getMonth() + 1;
  const DD = date.getDate();
  result = `${YY}-${MM > 10 ? MM : "0" + MM}-${DD > 10 ? DD : "0" + DD}`;
  return result;
}

export function transPlayCount(playCount?: number) {
  let res = "";
  if (typeof playCount !== "number") {
    return res;
  }
  if (playCount < 10000) {
    res = String(playCount);
  } else if (playCount < 100000000 && playCount >= 10000) {
    res = `${Math.ceil(playCount / 10000)}万`;
  } else if (playCount >= 100000000) {
    res = `${Math.ceil(playCount / 100000000)}亿`;
  }
  return res;
}

export function formatDt(dt: number) {
  const min = Math.floor(dt / (60 * 1000));
  const sec = Math.floor((dt % (60 * 1000)) / 1000);

  return `${min >= 10 ? min : "0" + min}:${sec >= 10 ? sec : "0" + sec}`;
}

export function formatCreators(
  crs: Array<{
    alias: any;
    id: number;
    name: string;
    tns: any;
  }>
) {
  if (crs instanceof Array) {
    return crs.reduce((total, curr, currIndex) => {
      return `${total}${currIndex !== 0 ? "/" : ""}${curr.name}`;
    }, "");
  }
}

export function throttle(fn: () => any, wait: number) {
  let lastTime = performance.now();

  return function (...args) {
    const thisTime = performance.now();
    if (thisTime - lastTime > wait) {
      lastTime = thisTime;
      fn.apply(this, [...args]);
    }
  };
}

export function debounce(fn: () => any, wait: number) {
  let timer: any = null;

  return function (...args) {
    if (timer) {
      clearTimeout(timer);
    }

    timer = setTimeout(() => {
      fn.apply(this, [...args]);
    }, wait);
  };
}

export function hasLogined() {
  if (window.localStorage.getItem("appCookie")) {
    return true;
  } else {
    return false;
  }
}
